package com.jinshan.gaietymall.controller;


import com.jinshan.gaietymall.core.exception.BusinessException;
import com.jinshan.gaietymall.entity.TMenu;
import com.jinshan.gaietymall.entity.request.TUserReqVO;
import com.jinshan.gaietymall.service.ITMenuService;
import com.jinshan.gaietymall.utils.BasePageResponseResult;
import com.jinshan.gaietymall.utils.BaseResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author liujinshan
 * @since 2020-04-15
 */
@RestController
    @RequestMapping("/admin/menu")
public class TMenuController {

    @Autowired
    private ITMenuService itMenuService;

    /**
      * @description 新增菜单
      * @author liujinshan
      * @date 2020/4/24 15:26
      */
    @PostMapping("add")
    public BaseResponseResult<TMenu> add(TMenu tMenu){
        BaseResponseResult<TMenu> baseResponseResult = new BaseResponseResult<>();
        try {
            baseResponseResult = BaseResponseResult.ok(itMenuService.save(tMenu));
        } catch (BusinessException e) {
            baseResponseResult.setErrCode(e.getCode());
            baseResponseResult.setErrMsg(e.getMsg());
        } catch (Exception e) {
            baseResponseResult.setErrCode(e.toString());
            baseResponseResult.setErrMsg("9999");
        }
        return baseResponseResult;
    }


    /**
      * @description 更新 
      * @author liujinshan
      * @date 2020/4/24 15:43
      */
    @PostMapping("update")
    public BaseResponseResult<TMenu> update(TUserReqVO tMenuVO){
        BaseResponseResult<TMenu> responseResult = new BaseResponseResult<>();
        try {

            responseResult = BaseResponseResult.ok(itMenuService.update());

        } catch (BusinessException e) {
            responseResult.setErrCode(e.getCode());
            responseResult.setErrMsg(e.getMsg());
        } catch (Exception e) {
            responseResult.setErrCode(e.toString());
            responseResult.setErrMsg("9999");
        }
        return responseResult;
    }


    /**
      * @description 删除 
      * @author liujinshan
      * @date 2020/4/24 15:43
      */
    @PostMapping("delete")
    public BaseResponseResult<TMenu> delete(){
        BaseResponseResult<TMenu> responseResult = new BaseResponseResult<>();
        try {

            responseResult = BaseResponseResult.ok(itMenuService.removeById(1));

        } catch (BusinessException e) {
            responseResult.setErrCode(e.getCode());
            responseResult.setErrMsg(e.getMsg());
        } catch (Exception e) {
            responseResult.setErrCode(e.toString());
            responseResult.setErrMsg("9999");
        }
        return responseResult;
    }


    /**
      * @description 分页查询 
      * @author liujinshan
      * param null
      * @return 
      * @throws 
      * @date 2020/4/24 15:43
      */
    @PostMapping(value = "getMenuListInfo", produces = {"application/json"}, consumes = {"application/json"})
    public BasePageResponseResult<TMenu> getMenuListInfo(){
        BasePageResponseResult<TMenu> responseResult = new BasePageResponseResult<>();
        try {
            List list = itMenuService.list();
            responseResult = BasePageResponseResult.ok(list);

        } catch (BusinessException e) {
            responseResult.setErrCode(e.getCode());
            responseResult.setErrMsg(e.getMsg());
        } catch (Exception e) {
            responseResult.setErrCode(e.toString());
            responseResult.setErrMsg("9999");
        }
        return responseResult;
    }


    /**
      * @description 根据ID查询
      * @author liujinshan
      * param null
      * @return 
      * @throws 
      * @date 2020/4/24 15:43
      */
    @PostMapping("getById")
    public BaseResponseResult<TMenu> getById(){
        BaseResponseResult<TMenu> responseResult = new BaseResponseResult<>();
        try {
            responseResult = BaseResponseResult.ok(itMenuService.getById(1));
        } catch (BusinessException e) {
            responseResult.setErrCode(e.getCode());
            responseResult.setErrMsg(e.getMsg());
        } catch (Exception e) {
            responseResult.setErrCode(e.toString());
            responseResult.setErrMsg("9999");
        }
        return responseResult;
    }

}
